package cn.tang.crm.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class AdminServlet extends BaseServlet {
	private static final Logger logger = Logger.getLogger(AdminServlet.class);
	private static final long serialVersionUID = 1L;

	public void m(HttpServletRequest req, HttpServletResponse resp) {

	}

	public void logout(HttpServletRequest req, HttpServletResponse resp) {
		req.getSession().removeAttribute("username");
		req.getSession().removeAttribute("role");
		responseJson(resp, "1");
	}

	public void login(HttpServletRequest req, HttpServletResponse resp) {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String ret = adminService.login(username, password, req);

		if (logger.isDebugEnabled())
			logger.debug("login(" + username + "," + password + ") return :" + ret);
		responseJson(resp, ret);
	}

	public void addUser(HttpServletRequest req, HttpServletResponse resp) {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String roleID = req.getParameter("roleID");
		String ret = adminService.addUser(username, password, roleID);
		if (logger.isDebugEnabled())
			logger.debug("add user() return :" + ret);
		responseJson(resp, ret);
	}

	public void addUserRole(HttpServletRequest req, HttpServletResponse resp) {
		String roleName = req.getParameter("roleName");
		String roleDescription = req.getParameter("roleDescription");
		String ret = adminService.addUserRole(roleName, roleDescription);
		if (logger.isDebugEnabled())
			logger.debug("addUserRole() return :" + ret);
		responseJson(resp, ret);
	}
	
	public void addClientType(HttpServletRequest req, HttpServletResponse resp) {
		String clientTypeName = req.getParameter("clientTypeName");
		String clientTypeDescription = req.getParameter("clientTypeDescription");
		String ret = adminService.addClientType(clientTypeName, clientTypeDescription);
		if (logger.isDebugEnabled())
			logger.debug("add user() return :" + ret);
		responseJson(resp, ret);
	}

	public void addClientClass(HttpServletRequest req, HttpServletResponse resp) {
		String clientClassName = req.getParameter("clientClassName");
		String clientClassMiniTrade = req.getParameter("clientClassMiniTrade");
		String clientClassMaxTrade = req.getParameter("clientClassMaxTrade");
		String clientClassDescription = req.getParameter("clientClassDescription");
		String ret = adminService.addClientClass(clientClassName, clientClassMiniTrade, clientClassMaxTrade, clientClassDescription);
		if (logger.isDebugEnabled())
			logger.debug("add user() return :" + ret);
		responseJson(resp, ret);
	}

	public void getUserInfo(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		Object ret = adminService.getUserInfo(id);
		if (logger.isDebugEnabled())
			logger.debug("add user() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllUser(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.listAllUser();
		if (logger.isDebugEnabled())
			logger.debug("listAllUser() return :" + ret);
		responseJson(resp, ret);
	}

	public void resetPwd(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String ret = adminService.resetPwd(id);
		if (logger.isDebugEnabled())
			logger.debug("resetPwd() return :" + ret);
		responseJson(resp, ret);
	}

	public void updateUser(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String roleID = req.getParameter("roleID");
		String ret = adminService.updateUser(id, roleID);
		if (logger.isDebugEnabled())
			logger.debug("updateUser() return :" + ret);
		responseJson(resp, ret);
	}

	public void deleteUser(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String ret = adminService.deleteUser(id);
		if (logger.isDebugEnabled())
			logger.debug("deleteUser() return :" + ret);
		responseJson(resp, ret);
	}

	public void getRoleOption(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.getRoleOption();
		if (logger.isDebugEnabled())
			logger.debug("getRoleOption() return :" + ret);
		responseJson(resp, ret);
	}

	public void getClentClassOption(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.getClentClassOption();
		if (logger.isDebugEnabled())
			logger.debug("getClentClassOption() return :" + ret);
		responseJson(resp, ret);
	}

	public void getClientTypeOption(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.getClientTypeOption();
		if (logger.isDebugEnabled())
			logger.debug("getClientTypeOption() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllRole(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.listAllRole();
		if (logger.isDebugEnabled())
			logger.debug("listAllRole() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllClientType(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.listAllClientType();
		if (logger.isDebugEnabled())
			logger.debug("listAllClientType() return :" + ret);
		responseJson(resp, ret);
	}

	public void listAllClientClass(HttpServletRequest req, HttpServletResponse resp) {
		String ret = adminService.listAllClientClass();
		if (logger.isDebugEnabled())
			logger.debug("listAllClientClass() return :" + ret);
		responseJson(resp, ret);
	}

}
